<template>
  <div class="displacement-dashboard">
    <!-- 主工作区 -->
    <div class="workspace">
      <!-- 左侧导航面板 -->
      <cardBox class="left-wrapper" title="目录" icon="&#xe6f2;">
          <menuAll :CATEGROY="4" :stationId="stationId" @getList="getTreeID" menuHeight="calc(100vh - 260px)"
          :showRoleAdd="0"
          :showRoleDel="0"
          :showRoleEdit="0"
          />
      </cardBox>

      <!-- 中央可视化区域 -->
      <div class="visualization-area">
        <!-- 时间控制卡片 -->
        <div class="time-control-card" v-if="bwtImg !== ''">
          <div class="card-header">
            <div>
              <i class="el-icon-time"></i>
              <span>时段配置</span>
            </div>
            <div>
              <el-button type="primary" icon="el-icon-plus" @click="handleAddTime" size="mini">添加时段</el-button>
              <el-button type="success" icon="el-icon-refresh" @click="handleReloadDrawImage" size="mini">应用时段</el-button>
            </div>
          </div>
          <div class="time-controls">
            <div class="time-periods-container">
              <div class="time-period" v-for="(item,index) in timeListForm" :key="index">
                <div class="period-inputs">
                  <div>
                    <span class="period-label">时段{{ index + 1 }}</span>
                    <div class="input-field">
                      <label>开始日期</label>
                      <el-date-picker
                        v-model="item.dt"
                        type="date"
                        size="small"
                        value-format="yyyy/MM/dd"
                        placeholder="选择日期"
                        style="width: 160px;margin-right: 18px;"
                      >
                      </el-date-picker>
                      <label>分析时长</label>
                        <el-input-number 
                          v-model="item.length" 
                          size="small" 
                          :min="1" 
                          controls-position="right"
                          >
                        </el-input-number>
                        <span class="unit">天</span>
                    </div>
                  </div>
                  <div class="period-actions">
                    <el-tag v-if="index === 0" type="primary" size="mini">主时段</el-tag>
                    <el-link v-if="index > 0" type="danger" icon="el-icon-delete" :underline="false" size="mini"  @click="handleDelTime(index)">删除</el-link>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 可视化画布 -->
        <div class="visualization-canvas" v-loading="imageLoading" 
             element-loading-text="图形加载中，请稍后"
             element-loading-spinner="el-icon-loading">
          
          <div class="empty-state" v-if="bwtImg == ''">
            <noData showChart description="暂无变位图显示"/>
          </div>

          <div class="graph-container" v-else>
            <!-- SVG图形区域 -->
            <div class="svg-area">
              <div class="svg-controls-top">
                <div class="zoom-controls">
                  <el-tooltip content="放大" placement="top">
                    <el-button icon="el-icon-zoom-in" circle size="mini" @click="handleZoomIn"></el-button>
                  </el-tooltip>
                  <el-tooltip content="缩小" placement="top">
                    <el-button icon="el-icon-zoom-out" circle size="mini" @click="handleZoomOut"></el-button>
                  </el-tooltip>
                  <el-tooltip content="适应屏幕" placement="top">
                    <el-button icon="el-icon-rank" circle size="mini" @click="handleFitView"></el-button>
                  </el-tooltip>
                  <el-tooltip content="显示/隐藏测值" placement="top">
                    <el-button 
                      :type="operateForm.measured ? 'primary' : ''" 
                      icon="el-icon-data-line" 
                      circle size="mini"
                      @click="operateForm.measured = !operateForm.measured">
                    </el-button>
                  </el-tooltip>
                  <el-tooltip content="显示/隐藏图例" placement="top">
                    <el-button 
                      :type="operateForm.legend ? 'primary' : ''" 
                      icon="el-icon-tickets" 
                      circle size="mini"
                      @click="operateForm.legend = !operateForm.legend">
                    </el-button>
                  </el-tooltip>
                </div>
                
                <div class="display-controls">
                  <div class="opacity-control">
                    <span>底图透明度:</span>
                    <el-slider v-model="trsp" @change="handleChangeOpacity" :min="0" :max="100" style="width: 120px; margin: 0 10px;" show-input-size="mini"></el-slider>
                  </div>
                </div>
              </div>
              <!-- SVG画布 -->
              <div class="elegant-legend" v-show="operateForm.legend">
                  <div class="legend-items">
                    <div v-for="(item,index) in lineTypeList" :key="index" 
                        class="legend-item" 
                        :class="{ 'disabled': !item.show }"
                        @click="hanldeChangeScaleShow(item)">
                      <div class="color-indicator" :style="{ backgroundColor: item.color }">
                        <i class="el-icon-check" v-if="item.show"></i>
                      </div>
                      <span class="item-label">{{ item.lineText }}</span>
                    </div>
                  </div>
              </div>
              <div class="svg-wrapper" id="svgWrapper" ref="svgWrapper">
                <svg :width="`${svgW}px`" :height="`${svgH}px`" :viewBox="`0 0 ${svgW} ${svgH}`"                      id="svg" >
                  <!-- 绘制背景图 -->
                  <defs>
                    <pattern id="img1" patternUnits="userSpaceOnUse" :width="`${svgW}px`" :height="`${svgH}px`">
                      <image :href="bwtImg" x="0" y="0" :width="`${svgW}px`" :height="`${svgH}px`" :opacity="trsp/100"/>
                    </pattern> 
                  </defs>
                  <rect x="0" y="0" :width="svgW" :height="svgH" stroke="black" fill="url(#img1)" stroke-width="0"/>
                  
                  <!-- 绘制所有图形元素 -->
                  <g v-for="(item,index) in pointAndLine" :key="index">
                    <!-- 绘制测点 -->
                    <circle :cx="item.startX" :cy="item.startY" :r="item.pointSize" :fill="item.color" 
                            v-if="item.lineType == 1 && item.show" @click="handleClickPoint(item)"
                            class="svg-point" :class="{'svg-element-hover': hoverElement === item}"/>
                    <!-- 绘制线 -->
                    <line :x1="item.startX" :y1="item.startY" :x2="item.endX" :y2="item.endY" 
                          :stroke="item.color" :stroke-width="item.borderSize" 
                          v-if="item.lineType > 1 && item.lineType < 6 && item.show" 
                          @mouseover="svgMouseMove(item,$event)" @mouseleave="svgMouseLeave" 
                          @click="handleClickPoint(item)"
                          class="svg-line" :class="{'svg-element-hover': hoverElement === item}"/>
                    <!-- 绘制带箭头的线 -->
                    <defs>
                      <marker id="triangle" markerUnits="strokeWidth" markerWidth="5" markerHeight="4" 
                              refX="0" refY="2" orient="auto" >
                        <path d="M 0 0 L 5 2 L 0 4 z" :style="`fill:${item.color}`"/>
                      </marker>
                    </defs>
                    <path :d="`M ${item.startX},${item.startY} L ${item.endX},${item.endY}`" 
                          :stroke="item.color" :stroke-width="item.borderSize" fill="none"
                          style="marker-end: url(#triangle);" 
                          v-if="(item.lineType == 6 || item.lineType == 7) && item.show" 
                          @click="handleClickPoint(item)"
                          class="svg-path" :class="{'svg-element-hover': hoverElement === item}"/>
                  </g>
                  
                  <g v-for="(item,index) in textList" :key="index">
                    <!-- 绘制文字 -->
                    <text :x="item.x" :y="item.y" fill="#000">{{item.text}}</text>
                  </g>
                  
                  <g v-if="operateForm.measured">
                    <g v-for="(item,index) in pointLineTips" :key="index">
                      <text 
                        v-for="(p,pi) in item.vLs" :key="pi"
                        :x="item.x + 6" 
                        :y="item.y + 20 * pi" 
                        :font-size="pointTipsSet.fontSize" 
                        :fill="pointTipsSet.fontColor" 
                        class="svg-text"
                      >{{ p }}</text> 
                    </g>
                  </g>
                </svg>
                
                <!-- 测点信息提示框 -->
                <div class="point-tooltip" :style="`left:${positonX}px;top:${positonY}px;`" 
                     v-show="pointToolShow && vl">
                  <div class="tooltip-arrow"></div>
                  <div class="tooltip-content">
                    <div class="tooltip-header">
                      <i class="el-icon-info"></i>
                      <span>测点信息</span>
                    </div>
                    <div class="tooltip-body">
                      <div class="data-row" v-for="(item,index) in vl" :key="index">
                        <span class="data-value">{{ item }}</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧控制面板 -->
      <cardBox class="right-wrapper" title="图形控制" icon="&#xe708;">
        <div class="panel-content">
          <div class="control-section"  v-if="showBtnGroup">
               <div class="section-title">合矢量图选择</div>
              <div class="radio-box">
                <el-radio-group v-model="graphtype" @change="handleChangeCategory" size="mini">
                  <el-radio :label="3" border>合矢量</el-radio>
                  <el-radio :label="4" border>分量1</el-radio>
                  <el-radio :label="5" border>分量2</el-radio>
                  <el-radio :label="6" border>全部分量</el-radio>
                </el-radio-group>
                
              </div>
          </div>
          <!-- 线条样式控制 -->
          <div class="control-section">
            <div class="control-group">
               <div class="section-title">样式控制</div>
              <div class="control-item">
                 <label>测点选择</label>
                <el-select v-model="operateForm.lineType" placeholder="请选择" size="small" @change="handleChangeLineName" style="width:180px">
                  <el-option
                    v-for="item in lineTypeList"
                    :key="item.lineType"
                    :label="item.lineText"
                    :value="item.lineType">
                  </el-option>
                </el-select>
              </div>
              
              <div class="control-item">
                <label>线条颜色</label>
                <el-color-picker v-model="operateForm.lineColor" size="small" @change="handleChangeColor" style="vertical-align: middle;"></el-color-picker>
              </div>
              
              <div class="control-item">
                <label>线条粗细</label>
                <el-slider v-model="operateForm.borderSize" :min="1" :max="10" @input="handleChangeLine" :show-input="false" style="display:inline-block;width:150px;height: 32px;vertical-align: middle;" :step="1"
                  show-stops></el-slider>
                  <span style="margin-left: 16px;">{{ operateForm.borderSize }}px</span>
              </div>
              <!-- <div class="toggle-item">
                <label>显示图例</label>
                <el-switch v-model="operateForm.legend" active-color="#13ce66"></el-switch>
              </div> -->
              <div class="toggle-item">
                <label>显示测值</label>
                <div>
                  <el-switch v-model="operateForm.measured" active-color="#13ce66"></el-switch>
                <el-tooltip class="item" effect="dark" content="测值样式设置" placement="top-start">
                 <el-button type="primary" @click="handleSetText" size="mini" class="el-icon-s-tools" style="padding:4px;margin-left:10px;"></el-button>
                </el-tooltip>
                </div>
              </div>
              <div class="control-group">
              <el-button  size="mini" icon="el-icon-odometer" type="primary" plain style="width:100%">
                设置比例尺
              </el-button>
            </div>
            </div>
          </div>
          <!-- 数据集合 -->
          <div class="control-section">
            <div class="section-title">数据集合</div>
            <!-- 测点集合 -->
            <el-collapse v-model="activeName" accordion>
              <el-collapse-item :title="`测点 (${ pointLineTips.length })`" name="1">
                <div class="data-list">
                  <div class="data-item" v-for="item in pointLineTips" :key="item.code_ID">
                  <span class="data-name">{{ item.vLs[0] }}</span>
                  <el-button type="text" @click="handleEditVlsPosition(item)" v-if="showRoleEdit == 1" size="mini">编辑</el-button>
                </div>
                <div class="empty-data" v-if="pointLineTips.length === 0">
                  <span>无测点数据</span>
                </div>
                </div>
              </el-collapse-item>
              <el-collapse-item :title="`文字 (${ textList.length })`" name="2">
                <div class="data-list">
                <div class="data-item" v-for="item in textList" :key="item.code_ID">
                  <span class="data-name">{{ item.text }}</span>
                  <el-button type="text" @click="handleEditVlsPosition(item)" v-if="showRoleEdit == 1" size="mini">编辑</el-button>
                </div>
                <div class="empty-data" v-if="textList.length === 0">
                  <span>无文字数据</span>
                </div>
                </div>
              </el-collapse-item>
            </el-collapse>
          </div>
        </div>
      </cardBox>
    </div>

    <!-- 设置对话框 -->
    <el-dialog 
      title="测值显示设置" 
      v-dialogDrag 
      :visible.sync="pointTextDialog" 
      width="460px" 
      :before-close="handleCancelPointText"
      custom-class="point-text-dialog"
    >
      <div class="dialog-content">

        <!-- 设置表单区域 -->
        <el-form 
          ref="pointTextForm" 
          label-width="90px" 
          size="medium" 
          :model="pointTextForm"
        >
          <!-- 字体设置 -->
          <el-form-item label="字体样式" class="form-item-with-tip">
              <el-select 
                v-model="pointTextForm.fontFamily" 
                placeholder="请选择字体"
                class="full-width"
              >
                <el-option-group label="中文字体">
                  <el-option label="微软雅黑" value="微软雅黑">
                    <span style="font-family: 微软雅黑">微软雅黑</span>
                  </el-option>
                  <el-option label="黑体" value="黑体">
                    <span style="font-family: 黑体">黑体</span>
                  </el-option>
                  <el-option label="宋体" value="宋体">
                    <span style="font-family: 宋体">宋体</span>
                  </el-option>
                  <el-option label="楷体" value="楷体">
                    <span style="font-family: 楷体">楷体</span>
                  </el-option>
                  <el-option label="仿宋" value="仿宋">
                    <span style="font-family: 仿宋">仿宋</span>
                  </el-option>
                </el-option-group>
                <el-option-group label="英文字体">
                  <el-option label="Arial" value="Arial">
                    <span style="font-family: Arial">Arial</span>
                  </el-option>
                  <el-option label="Times New Roman" value="Times New Roman">
                    <span style="font-family: Times New Roman">Times New Roman</span>
                  </el-option>
                  <el-option label="Verdana" value="Verdana">
                    <span style="font-family: Verdana">Verdana</span>
                  </el-option>
                </el-option-group>
              </el-select>
            </el-form-item>

            <!-- 颜色设置 -->
            <el-form-item label="文字颜色" class="form-item-with-tip">
              <el-color-picker 
                v-model="pointTextForm.fontColor"
              ></el-color-picker>
            </el-form-item>

            <!-- 字体大小 -->
            <el-form-item label="字体大小" class="form-item-with-tip">
              <el-slider 
                v-model="pointTextForm.fontSize"
                :min="8"
                :max="24"
                :step="1"
                show-stops
                :show-tooltip="true"
                style="width:200px;display: inline-block;vertical-align: middle;"
              ></el-slider>
              <span style="vertical-align: middle; margin-left: 30px;">{{pointTextForm.fontSize}}px</span>
            </el-form-item>

            <!-- 字体样式 -->
            <el-form-item label="加粗" class="form-item-with-tip">
              <el-switch
                  v-model="pointTextForm.bold"
                  active-color="#13ce66"
                  inactive-color="#999"
                  :active-value="1"
                  :inactive-value="0"
                  >
                </el-switch>
            </el-form-item>
          </el-form>
        </div>
        <div  class="dialog-footer">
            <el-button 
              @click="handleCancelPointText" 
              size="small" 
              icon="el-icon-circle-close"
              class="cancel-btn"
            >
              取消
            </el-button>
            <el-button 
              type="primary" 
              @click="handleSavePointText" 
              size="small" 
              icon="el-icon-circle-check"
              class="confirm-btn"
            >
              保存设置
            </el-button>
        </div>
    </el-dialog>

    <el-dialog title="测点坐标配置"  v-dialogDrag :visible.sync="pointVlDialog" width="350px">
        <el-form ref="pointVlPosition" label-width="60px" size="medium" :model="pointVlPosition">
            <el-form-item label="X坐标">
              <el-input-number v-model="pointVlPosition.x"  :min="1"  label="请输入数字"  controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="Y坐标">
              <el-input-number v-model="pointVlPosition.y"  :min="1"  label="请输入数字"  controls-position="right"></el-input-number>
            </el-form-item>
        </el-form> 
        <div class="dialog-footer">
          <el-button 
            @click="pointVlDialog = false" 
            size="small" 
            icon="el-icon-circle-close"
            class="cancel-btn"
          >
            取消
          </el-button>
          <el-button 
            type="primary" 
            @click="handleSavePointVl" 
            size="small" 
            icon="el-icon-circle-check"
            class="confirm-btn"
          >
            保存设置
          </el-button>
        </div>
    </el-dialog>
    
  </div>
</template>

<script>
import cardBox from "@/components/base/cardBox.vue";
import menuAll from '@/components/page/informationAnalysis/menuAll.vue'
import {getLinkInfo,getBwtConfigsByTreeID,GraphPointDraw,setGraphFont,getGraphFont,setGraphPointFonts} from "@/api/https";
import noData from '@/components/base/noData.vue';

export default ({
    components:{cardBox,menuAll,noData},
    props:{
        stationId:{
            type:[String,Number],
            default:'',
        },
        showRoleAdd:{
          type:Number,
          default:0
        },
        showRoleEdit:{
          type:Number,
          default:0
        },
        showRoleDel:{
          type:Number,
          default:0
        }
    },
    data() {
        return{
            treeId:'',
            imageLoading:false,
            operateForm:{
                legend: true,
                measured: false,
                lineType: null,
                lineColor: '#409EFF',
                borderSize: 2
            },
            lineTypeList:[],
            svgW:800,
            svgH:600,
            bwtImg:'',
            trsp:100,
            pointAndLine:[],
            textList:[],
            pointToolShow:false,
            positonY:'',
            positonX:'',
            vl:'',
            timeListForm:[],
            graphtype:'',
            svgTreeID:'',
            pointLineTips:[],
            pointTipsSet:{},
            pointTextForm:{},
            pointTextDialog:false,
            imageRef:null,
            hoverElement: null,
            currentScale: 1,
            minScale: 0.1,
            maxScale: 10,
            pointVlPosition:{},
            pointVlDialog:false,
            showBtnGroup:false,
            activeName:'1'
        }
    },
    beforeDestroy(){
      if (this.imageRef) {
          this.imageRef.onload = null;
          this.imageRef = null;
      }
    },
    methods:{
        getTreeID(treeId,data){
            const list = []
            this.treeId = treeId
            this.graphtype = data.graphtype || data.node.graphtype || null
            if( this.graphtype == 3){
              this.showBtnGroup = true
            }else{
              this.showBtnGroup = false
            }
            if(data.b_Time != null && data.t_Len != null){
                list.push({
                    dt:data.b_Time,
                    length:data.t_Len
                })
            }else{
                const date = new Date()
                const toDay = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate()
                list.push({
                    dt:toDay,
                    length:365
                })
            }
            this.timeListForm = [...list]
            this.getDisplacementData()
        },
        getDisplacementData(){
          this.imageLoading = true;
          if (this.imageRef) {
              this.imageRef.onload = null;
              this.imageRef = null;
          }
          // 获取底图
          getLinkInfo({
            PointTreeID: this.treeId
          }).then(res => {
            if (res.isSuccess && res.data && res.data.imgUrl) {
              this.imageRef = new Image();
              this.imageRef.src = res.data.imgUrl;
              this.bwtImg = res.data.imgUrl || '';
              
              this.imageRef.onload = () => {
                // 获取容器尺寸（减去内边距）
                const container = this.$refs.svgWrapper;
                const padding = 10; // 10px内边距
                const availableWidth = (container?.offsetWidth || 800) - padding * 2;
                const availableHeight = (container?.offsetHeight || 600) - padding * 2;
                
                const imageWidth = this.imageRef.width;
                const imageHeight = this.imageRef.height;
                
                // 计算缩放比例，确保图片完全适应容器且不超出
                const scaleX = availableWidth / imageWidth;
                const scaleY = availableHeight / imageHeight;
                const scale = Math.min(scaleX, scaleY); // 取最小比例确保完全显示
                
                // 计算缩放后的尺寸
                let width = imageWidth * scale;
                let height = imageHeight * scale;
                
                // 确保尺寸不超过可用空间
                width = Math.min(width, availableWidth);
                height = Math.min(height, availableHeight);
                
                // 设置SVG尺寸（取整）
                this.svgW = Math.floor(width);
                this.svgH = Math.floor(height);
                
                this.handleGetGraphPointDraw();
                
                //  获取文字
                getBwtConfigsByTreeID({
                    treeID: this.treeId,
                    width: this.svgW,
                    height: this.svgH,
                    dt: '',
                    length: ''
                }).then(res => {
                    if (res.isSuccess) {
                        this.textList = res.data;
                    } else {
                        this.textList = [];
                    }
                });
              }
              this.imageRef.onerror = () => {
                this.imageLoading = false;
                this.$message.error('获取底图信息失败');
                this.resetDisplayData();
              };
              } else {
                this.imageLoading = false;
                this.resetDisplayData();
              }
          }).catch(error => {
              this.imageLoading = false;
              this.bwtImg = '';
              this.resetDisplayData();
          });
        },
        resetDisplayData() {
            this.pointLineTips = [];
            this.pointAndLine = [];
            this.lineTypeList = [];
            this.bwtImg = '';
            this.operateForm = {
                legend: true,
                measured: false,
                lineType: null,
                lineColor: '#409EFF',
                borderSize: 2
            };
        },
        handleGetGraphPointDraw(){
            GraphPointDraw({
                pointTreeID:this.treeId,
                graphtype:this.graphtype,
                w:this.svgW,
                h:this.svgH,
                timeParts:this.timeListForm
            }).then(res=>{
                if(res.isSuccess && res.data && res.data.drawInfos){
                    this.svgTreeID = res.data.treeID
                    const listmap = []
                    res.data.drawInfos.forEach(item=>{
                        const _data = {
                            borderSize:item.borderSize > 0 ? item.borderSize : 2,
                            color:item.color,
                            endX:item.endX,
                            endY:item.endY,
                            ifBorder:item.ifBorder,
                            lineType:item.lineType,
                            pointName:item.pointName,
                            pointSize:item.pointSize,
                            startX:item.startX,
                            startY:item.startY,
                            vl:item.vl,
                            show:true,
                            code_ID:item.code_ID
                        }              
                        listmap.push(_data)
                    })
                    this.pointLineTips = res.data.vLs  
                    this.pointTipsSet = res.data.graphFont
                    this.trsp = res.data.graphFont.trsp || 100
                    this.pointAndLine = [...listmap]
                    let _lineTypeList = []
                    res.data.typeIllustrates.forEach(item=>{
                      const color = res.data.drawInfos.filter(s=> s.lineType == item.lineType)
                      _lineTypeList.push({
                          lineType:item.lineType,
                          lineText:item.lineText,
                          color:color.length > 0 ? color[0].color : '#999',
                          show:true
                        })
                    })
                    
                    this.lineTypeList = [..._lineTypeList];
                    // 设置默认选中的线类型
                    if (this.lineTypeList.length > 0 && !this.operateForm.lineType) {
                        this.operateForm.lineType = this.lineTypeList[0].lineType
                        this.operateForm.lineColor = this.lineTypeList[0].color
                    }
                }else{
                    this.resetDisplayData()
                    this.$message.error(res.errorMessage)
                }
                this.imageLoading = false
            }).catch(err=>{
                this.imageLoading = false
                this.resetDisplayData()
            })
        },
        
        // 缩放功能
        handleZoomIn() {
            if (this.currentScale < this.maxScale) {
                this.currentScale = Math.min(this.currentScale + 0.1, this.maxScale);
                this.applyScale();
            }
        },
        handleZoomOut() {
            if (this.currentScale > this.minScale) {
                this.currentScale = Math.max(this.currentScale - 0.1, this.minScale);
                this.applyScale();
            }
        },

        handleFitView() {
            this.currentScale = 1;
            this.applyScale();
        },

        applyScale() {
            const svgElement = document.getElementById('svg');
            if (svgElement) {
                svgElement.style.transform = `scale(${this.currentScale})`;
                svgElement.style.transformOrigin = 'center center';
            }
        },
        handleChangeColor(){
            this.pointAndLine.forEach(item=>{
                if(item.lineType == this.operateForm.lineType){
                    item.color = this.operateForm.lineColor
                }
            })

            this.lineTypeList.forEach(item=>{
                if(item.lineType == this.operateForm.lineType){
                    item.color = this.operateForm.lineColor
                }
            })
        },
        handleChangeLineName(){
            this.pointAndLine.forEach(item=>{
                if(item.lineType == this.operateForm.lineType){
                    this.operateForm.lineColor = item.color || '#409EFF'
                    this.operateForm.borderSize = item.borderSize || 2
                }
            })
        },
        handleChangeLine(){
            this.pointAndLine.forEach(item=>{
                if(item.lineType == this.operateForm.lineType){
                    item.borderSize = this.operateForm.borderSize
                }
            })
        },
        hanldeChangeScaleShow(data){
            data.show = !data.show
            this.pointAndLine.forEach(item=>{
                if(item.lineType == data.lineType){
                    item.show = data.show
                }
            })
        },
        handleAddTime(){
            this.timeListForm.push({
                dt:'',
                length:1
            })
        },
        handleDelTime(index){
            this.timeListForm.splice(index,1)
        },
        handleReloadDrawImage(){
            this.getDisplacementData()
        },
        handleSetText(){
            this.pointTextDialog = true
            getGraphFont({
                pointTreeID:this.treeId
            }).then(res=>{
                if(res.isSuccess && res.data){
                    this.pointTextForm = res.data
                }
            })
        },
        handleCancelPointText(){
            this.pointTextDialog = false
            this.pointTextForm = {}
        },
        handleSavePointText(){
            const data = {...this.pointTextForm}
            data.pointTreeID = this.treeId
            data.trsp = this.trsp
            setGraphFont(data).then(res=>{
                if(res.isSuccess){
                    this.pointTextDialog = false
                    this.pointTipsSet = {...this.pointTextForm}
                }else{
                    this.$message.error(res.errorMessage)
                }
            })
        },
        handleChangeOpacity(val){
            const data = {...this.pointTextForm}
            data.pointTreeID = this.treeId
            data.trsp = val
            setGraphFont(data).then(res=>{
                if(res.isSuccess){
                    // 透明度更新成功
                }else{
                    this.$message.error(res.errorMessage)
                }
            })
        },
        svgMouseMove(data,evt){
          // 鼠标悬停，悬浮窗展示相关信息
          this.pointToolShow = data.vl != '' && data.vl != null ? true : false
          this.vl = data.vl != '' && data.vl != null ? data.vl.split(',') : ''
          this.positonX = evt.offsetX
          this.positonY = evt.offsetY
      },
      svgMouseLeave(){
        this.pointToolShow = false
        this.vl = ''
      },
      handleEditVlsPosition(data){
        this.pointVlPosition = {...data}
        this.pointVlDialog = true
      },
      handleSavePointVl(){
        const data = {...this.pointVlPosition}
        data.w = this.svgW
        data.h = this.svgH
        setGraphPointFonts(data).then(res=>{
          if(res.isSuccess){
           this.handleGetGraphPointDraw()

            this.pointVlDialog = false
            this.pointVlPosition = {}
          }else{
            this.$message.error(res.errorMessage)
          }
        })
      },
    }
})
</script>

<style scoped lang="scss">
.displacement-dashboard {
  height: calc(100vh - 120px);
  overflow: hidden;
  .workspace {
    display: grid;
    grid-template-columns: 340px 1fr 300px;
    height: calc(100vh - 170px);
    overflow: hidden;
    gap: 10px;
    .left-wrapper{
        width: 340px;
        height: calc(100vh - 185px);
        flex-shrink: 0;
        margin-top:10px;
    }
    .right-wrapper{
        height: calc(100vh - 185px);
        overflow: hidden;
        margin-top:10px;
    }
  }
}

.visualization-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  margin-top:10px;

  .time-control-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.8);

    .card-header {
      display: flex;
      justify-content: space-between;
      gap: 8px;
      padding: 10px 20px;
      border-bottom: 1px solid #f0f0f0;

      i {
        color: #409EFF;
        font-size: 16px;
        margin-right: 6px;
      }

      span {
        font-weight: 600;
        color: #303133;
      }
    }

    .time-controls {
      padding: 10px;

      .time-periods-container {
        display: flex;
        flex-direction: column;
        gap: 8px;
        max-height: 130px;
        overflow-y: auto;

        .time-period {
          background: #f8f9fa;
          border-radius: 6px;
          padding: 10px;
          border: 1px solid #e9ecef;

          .period-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;

            
          }

          .period-inputs {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            .period-label {
              font-weight: 600;
              color: #495057;
              margin-right: 30px;
            }
            .input-field {
              display: inline-block;
              label {
                font-size: 12px;
                color: #6c757d;
                margin-right:12px;
              }
            }
          }
        }
      }
    }
  }

  .visualization-canvas {
    flex: 1;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.8);
    position: relative;
    overflow: auto;
    height: calc(100vh - 900px);
    .empty-state{
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .graph-container {
      height:calc(100vh - 350px);
      overflow: hidden;
      .svg-area {
        display: flex;
        flex-direction: column;
        height: 100%;
        .svg-controls-top {
          height: 63px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 12px 20px;
          background: rgba(255, 255, 255, 0.9);
          backdrop-filter: blur(10px);
          border-bottom: 1px solid #f0f0f0;

          .zoom-controls {
            display: flex;
            gap: 8px;
          }

          .display-controls {
            .opacity-control {
              display: flex;
              align-items: center;
              gap: 8px;

              span {
                font-size: 13px;
                color: #495057;
                white-space: nowrap;
              }
            }
          }
        }

        .svg-wrapper {
          width: 100%;
          flex:1;
          overflow: hidden;
          background: #f8f9fa;
          margin:0 auto;
          svg {
            transition: transform 0.3s ease;
            width: 100%;
            height: 100%;
          }

          /* SVG元素悬停效果 */
          .svg-point:hover,
          .svg-line:hover,
          .svg-path:hover,
          .svg-text:hover {
            cursor: pointer;
          }

          .svg-element-hover {
            filter: drop-shadow(0 0 2px rgba(64, 158, 255, 0.8));
          }

          .point-tooltip {
            position: absolute;
            z-index: 100;
            pointer-events: none;

            .tooltip-arrow {
              width: 0;
              height: 0;
              border-left: 6px solid transparent;
              border-right: 6px solid transparent;
              border-bottom: 6px solid rgba(0, 0, 0, 0.85);
              margin: 0 auto;
            }

            .tooltip-content {
              background: rgba(0, 0, 0, 0.85);
              border-radius: 6px;
              box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
              color: white;
              min-width: 180px;

              .tooltip-header {
                padding: 8px 12px;
                border-bottom: 1px solid rgba(255, 255, 255, 0.2);
                display: flex;
                align-items: center;
                gap: 6px;
                font-size: 13px;
                font-weight: 500;
              }

              .tooltip-body {
                padding: 8px 12px;

                .data-row {
                  padding: 4px 0;

                  .data-value {
                    font-size: 12px;
                    line-height: 1.4;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}



.empty-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #6c757d;

  .empty-content {
    text-align: center;

    i {
      font-size: 48px;
      margin-bottom: 12px;
      opacity: 0.5;
    }

    p {
      margin: 0 0 4px;
      font-size: 14px;
      color: #495057;
    }

    span {
      font-size: 12px;
    }
  }
}

.full-width {
  width: 100%;
}

.btn-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.color-preview {
  margin-left: 10px;
  font-size: 14px;
}

.unit {
  margin-left: 8px;
  font-size: 12px;
  color: #909399;
}

.drawer-content {
  padding: 20px;
  
  .scale-form {
      .point-inputs, .distance-inputs {
          display: flex;
          align-items: center;
          flex-wrap: wrap;
          gap: 10px;
      }
      
      .point-hint {
          margin: 5px 0 0;
          font-size: 12px;
          color: #909399;
      }
  }
  
  .drawer-actions {
      display: flex;
      justify-content: center;
      gap: 15px;
      margin-top: 30px;
  }
}



.panel-content{
  padding: 12px;
  box-sizing: border-box;
  height: 100%;
  overflow-y: auto;
  
  .control-section {
    margin-bottom: 16px;
    ::v-deep .el-collapse-item__header{
      font-size: 14px;
      color: #303133;
      font-weight: 500;
    }
    .section-title {
      font-size: 14px;
      color: #303133;
      font-weight: 600;
      margin-bottom: 8px;
      padding-bottom: 4px;
      border-bottom: 1px solid #f0f0f0;
    }

    .control-group {
      .control-item {
        margin-bottom: 6px;

        label {
          display: inline-block;
          font-size: 13px;
          color: #606266;
          margin-right: 10px;
        }
      }
      .toggle-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 6px;
        height: 32px;
        label {
          display: inline-block;
          font-size: 13px;
          color: #606266;
          margin-right: 10px;
        }
      }
    }
  }
}
      
  .data-list {
    background: #f8f9fa;
    max-height:calc(50vh - 350px);
    overflow-y: auto;
    
    .data-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 4px 6px;
      border-bottom: 1px solid #e9ecef;
      background: white;
      border-radius: 2px;
      margin-bottom: 2px;
      
      .data-name {
        font-size: 12px;
        color: #303133;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
      }
    }
    
    .empty-data {
      text-align: center;
      padding: 8px 0;
      color: #909399;
      font-size: 12px;
    }
  }


.elegant-legend {
  background: #fff;
  padding: 12px;  
  height: 56px;
  box-sizing: border-box;
  .legend-title {
    font-size: 14px;
    font-weight: 600;
    color: #2d3748;
    margin-bottom: 10px;
    padding-bottom: 6px;
    border-bottom: 1px solid #edf2f7;
  }
  
  .legend-items {
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 6px;
    
    .legend-item {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 6px 8px;
      border-radius: 6px;
      cursor: pointer;
      transition: all 0.3s ease;
      border: 1px solid transparent;
      
      &:hover {
        background: #f7fafc;
        border-color: #e2e8f0;
        transform: translateY(-1px);
      }
      
      &.disabled {
        opacity: 0.5;
        
        .color-indicator {
          background-color: #cbd5e0 !important;
          
          i {
            display: none;
          }
        }
        
        .item-label {
          color: #a0aec0;
        }
      }
      
      .color-indicator {
        width: 16px;
        height: 16px;
        border-radius: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        transition: all 0.3s ease;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        
        i {
          color: white;
          font-size: 10px;
          font-weight: bold;
          text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
        }
        
        &::before {
          content: '';
          position: absolute;
          top: -2px;
          left: -2px;
          right: -2px;
          bottom: -2px;
          border-radius: 6px;
          background: inherit;
          opacity: 0.3;
          z-index: -1;
        }
      }
      
      .item-label {
        font-size: 13px;
        color: #4a5568;
        font-weight: 500;
        transition: color 0.3s ease;
      }
    }
  }
}

/* 测点样式优化 */
.svg-point {
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
  
  &:hover {
    r: 8; /* 悬停时放大 */
    filter: drop-shadow(0 4px 8px rgba(64, 158, 255, 0.4));
    stroke: white;
    stroke-width: 2;
  }
}

/* 线条样式优化 */
.svg-line, .svg-path {
  &:hover {
    stroke-width: calc(var(--base-stroke-width, 2) + 1);
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
  }
}

/* 文字样式优化 */
.svg-text {
  font-family: 'Microsoft YaHei', sans-serif;
  paint-order: stroke;
  stroke: white;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  
  &:hover {
    fill: #409EFF;
    font-weight: bold;
  }
}

.radio-box{
  ::v-deep .el-radio.is-bordered+.el-radio.is-bordered{
   margin:0px 6px 6px 0;
  }
  ::v-deep .el-radio{
  margin:0px 6px 6px 0;
  }
}
</style>